package com.hx.zhg.web.util;

import com.hx.zhg.service.system.vo.JSSDKConfigVo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * request工具类
 */
public class RequestUtil {

    private static final String USER_AGENT = "user-agent";
    private static final String REFERER = "Referer";

    /**
     * 获取referrer
     */
    public static String getReferrer(HttpServletRequest request) {
        String referrer = StringUtils.trim(request.getHeader(REFERER));
        return referrer;
    }

    /**
     * 获取user-agent
     */
    public static String getUserAgent(HttpServletRequest request) {
        String userAgent = request.getHeader(USER_AGENT);
        return userAgent;
    }

    /**
     * 获取当前URL（包括域名和协议）
     *
     * @param request
     * @return
     */
    public static String getCurrUrl(String contextPath, HttpServletRequest request) {
        StringBuffer sb = new StringBuffer();
        sb.append(request.getScheme());
        sb.append("://");
        sb.append(request.getServerName());
        if (request.getServerPort() != 80) {
            sb.append(":" + request.getServerPort());
        }
        if (!"/".equals(contextPath)) {
            sb.append(contextPath);
        }
        sb.append(request.getServletPath());
        return sb.toString();
    }

    /**
     * 设置JSSDKConfig
     *
     * @param request
     * @param jssdkConfigVo
     */
    public static void setJSSDKConfig(HttpServletRequest request, JSSDKConfigVo jssdkConfigVo) {
        request.setAttribute("appId", jssdkConfigVo.getAppId());
        request.setAttribute("timestamp", jssdkConfigVo.getTimestamp());
        request.setAttribute("nonceStr", jssdkConfigVo.getNonceStr());
        request.setAttribute("signature", jssdkConfigVo.getSignature());
    }

}
